package com.company.ThreadPoolDemo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 作者  Administrator
 * 时间  2017/6/2 10:18
 * 文件  ThreadPoolsDemo
 * 描述  Fix 固定的意思
 */
public class FixThreadPool {
	public static void main(String[] args) {
//		fixThread();
		sigleThread();
	}

	private static void fixThread() {
		// 固定线程为3个 核心线程不销毁
		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
		for (int i = 1; i <= 10; i++) {
			final int index = i;
			fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					String threadName = Thread.currentThread().getName();
					System.out.println("线程："+threadName+",正在执行第" + index + "个任务");
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}
	}

	public static void sigleThread(){
		ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
		for (int i = 1; i <= 10; i++) {
			final int index = i;
			singleThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					String threadName = Thread.currentThread().getName();
					System.out.println("线程："+threadName+",正在执行第" + index + "个任务");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}
	}
}
